package com.syh.jn.scm.service.impl.operation.system;

import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.operation.entity.system.DictType;
import com.syh.jn.scm.domain.operation.repository.system.DictTypeRepository;
import com.syh.jn.scm.dto.operation.system.dict.DictTypeMto;
import com.syh.jn.scm.dto.operation.system.dict.DictTypeSto;
import com.syh.jn.scm.dto.operation.system.dict.DictTypeVo;
import com.syh.jn.scm.service.api.operation.system.DictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Date:2018/7/12
 * Author:Clown
 * 字典类型分类的seviceImpl;
 */
@Service
@Transactional
public class DictTypeServiceImpl extends AbstractServiceImpl<DictTypeSto, DictTypeMto, DictTypeVo, DictType, Long> implements DictTypeService {
  @Autowired
  private DictTypeRepository dictTypeRepository;

  @Override
  protected AggregateRepository<DictType,Long> getAggregateRepository() { return dictTypeRepository; }

  /**
   * 保存对象
   *
   * @param sto
   * @return
   */
  @Override
  public Long save(DictTypeSto sto){
    DictType dictType = DictType.createDictType(sto);
    dictTypeRepository.add(dictType);
    return dictType.getId();
  }

  /**
   * 修改对象
   *
   * @param mto
   * @return
   */
  @Override
  public void update(DictTypeMto mto){
    DictType dictType = dictTypeRepository.getById(mto.getId());
    dictType.modify(mto);
    dictTypeRepository.update(dictType);
  }

  /**
   * 根据id删除对象
   *
   * @param id
   */
  @Override
  public void delete(Long id) {
    DictType dictType = dictTypeRepository.getById(id);
    dictType.destroy();
    dictTypeRepository.remove(dictType);
  }

  /**
   * 启用
   * @param id
   */
  @Override
  public void enabled(Long id) {
    this.recursionEnabled(id);
  }

  /**
   * 递归启用
   *
   * @param id
   */
  private void recursionEnabled(Long id) {
    DictType dictType = dictTypeRepository.getById(id);
    //先启用父
    if (!Long.valueOf("0").equals(dictType.getSupId())) {
      DictType categorySup = dictTypeRepository.getById(dictType.getSupId());
      categorySup.enabled();
      dictTypeRepository.update(categorySup);
      recursionEnabled(categorySup.getId());
    }
    //再启用子
    dictType.enabled();
    dictTypeRepository.update(dictType);
  }

  /**
   * 停用
   * @param id
   */
  @Override
  public void disabled(Long id) {
    this.recursionDisabled(id);
    DictType dictType = dictTypeRepository.getById(id);
    dictType.disabled();
    dictTypeRepository.update(dictType);
  }

  /**
   * 递归停用子类
   *
   * @param id
   */
  private void recursionDisabled(Long id) {
    DictType dictType = dictTypeRepository.getById(id);
    //先把所有子停用
    Wrapper cateWrapper = Wrapper.build();
    cateWrapper.append(Restrictions.eq("supId", id));
    List<DictType> dictTypeList = dictTypeRepository.getList(cateWrapper);
    if (dictTypeList != null && dictTypeList.size() > 0) {
      dictTypeList.stream().forEach((dictTypeTemp) -> {
        recursionDisabled(dictTypeTemp.getId());
        dictTypeTemp.disabled();
        dictTypeRepository.update(dictTypeTemp);
      });
    }
    //再停用父的
    dictType.disabled();
    dictTypeRepository.update(dictType);
  }
}